#ifdef WIN32
#include <windows.h>
#include "win_clipboard.hpp"


windows_clipboard::windows_clipboard(itoken* _token): clipboard(_token)
{}

windows_clipboard::windows_clipboard(itoken* _token, std::vector<unsigned short>& _idx): clipboard(_token, _idx)
{}


int windows_clipboard::clear_buffer()
{
	if(::OpenClipboard(NULL))
	{
		::EmptyClipboard();
	}

	return ::CloseClipboard();
}

/*
 * Copies the given string TO the clipboard.
 */
int windows_clipboard::write_buffer()
{	
	//std::string _ptr(encrypt_aes::decrypt_from(cipher_filename, key_filename, iv_filename, substring_positions));

std::string _ptr = (indices.empty()) ? token->read() : token->read(indices);

	char* buffer;   
   HGLOBAL clipbuffer;
   
   	// test to see if we can open the clipboard first before
  	// wasting any cycles with the memory allocation
   	if(::OpenClipboard(NULL))
   	{
   
   		// Ok. We have the Clipboard locked and it's empty. 
   		// Now let's allocate the global memory for our data.

   		// Here I'm simply using the GlobalAlloc function to 
   		// allocate a block of data equal to the text in the
   		// "to clipboard" edit control plus one character for the
   		// terminating null character required when sending
   		// ANSI text to the Clipboard.
	
    	// Empty the Clipboard. This also has the effect
   		// of allowing Windows to free the memory associated
   		// with any data that is in the Clipboard
	   	::EmptyClipboard();
		clipbuffer = ::GlobalAlloc(GMEM_DDESHARE, _ptr.length()+1);

		// Calling GlobalLock returns to me a pointer to the 
   		// data associated with the handle returned from 
   		// GlobalAlloc
		buffer = static_cast<char*>(::GlobalLock(clipbuffer));

		// At this point, all I need to do is use the standard 
   		// C/C++ strcpy function to copy the data from the local 
   		// variable to the global memory.
		::strcpy(buffer, _ptr.c_str());

		// Once done, I unlock the memory - remember you 
   		// don't call GlobalFree because Windows will free the 
   		// memory automatically when EmptyClipboard is next 
   		// called.
	   	::GlobalUnlock(clipbuffer);

		// Now, set the Clipboard data by specifying that 
   		// ANSI text is being used and passing the handle to
   		// the global memory.
	   	::SetClipboardData(CF_TEXT,clipbuffer);
   }


	// Finally, when finished I simply close the Clipboard
   	// which has the effect of unlocking it so that other
   	// applications can examine or modify its contents.
   	return ::CloseClipboard();
}


// private method
/*
 * Suppose there is a string on the clipboard.
 * This function copies it FROM there.
 */
int windows_clipboard::read_buffer()
{
   char* buffer = NULL;  
   HANDLE hData;
   
   if(::OpenClipboard(NULL))
   {     
   		if (::IsClipboardFormatAvailable(CF_TEXT) || ::IsClipboardFormatAvailable(CF_OEMTEXT))
   		{ 
	   		// Retrieve the Clipboard data (specifying that 
    		// we want ANSI text (via the CF_TEXT value).
			hData = ::GetClipboardData( CF_TEXT );

			// Call GlobalLock so that to retrieve a pointer
    		// to the data associated with the handle returned
    		// from GetClipboardData.
			buffer = (char*)::GlobalLock( hData );

			// Unlock the global memory.
			::GlobalUnlock( hData );		
   		}
   	// da rivedere l'allocazione di memoria	
		::fprintf(stderr, buffer);
   }
   
   	// Finally, when finished I simply close the Clipboard
	// which has the effect of unlocking it so that other
  	// applications can examine or modify its contents.

   return ::CloseClipboard();
}


#endif
